package com.xy.iot.controller;

import com.xy.iot.common.result.R;
import com.xy.iot.common.utils.CommonUtil;
import com.xy.iot.service.SerialPortService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.nio.charset.StandardCharsets;

/**
 * 串口相关接口
 */
@Slf4j
@RestController
@RequestMapping("api/serialPort/")
public class SerialPortController {
    @Autowired
    private SerialPortService serialPortService;
    /**
     * 串口列表
     * @return
     */
    @RequestMapping("list")
    public R serialPortList() {
        return R.ok(serialPortService.getAllSerialPort());
    }

    /**
     * 串口发送数据
     * @return
     */
    @PostMapping("sendData")
    public R sendData(@RequestParam(name = "sendData", required = false,defaultValue = "111") String sendData) {
        log.info("sendData：{}",sendData);
        String data = CommonUtil.encodeHexStr(sendData);
        log.info("encodeHexStr：{}",data);
        log.info("decodeHexStr：{}",CommonUtil.decodeHexStr(data));
        serialPortService.serialPortSendData(data.getBytes(StandardCharsets.UTF_8));
        return R.ok();
    }

}
